package com.epam.properties;

import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.epam.factories.TypeOfAircraft;

public class PropertyFilePicker {
	private Path path = Paths.get(System.getProperty("user.dir")
			+ "\\src\\com\\epam\\properties");
	private List<String> FileNameList = new ArrayList<String>();
	private TypeOfAircraft typeOfAircraft;

	public PropertyFilePicker(TypeOfAircraft typeOfAircraft) {
		try {
			this.typeOfAircraft=typeOfAircraft;
			if (typeOfAircraft==TypeOfAircraft.AIRPLANE)
				path = Paths.get(path+"\\Airplanes");
			else if (typeOfAircraft==TypeOfAircraft.HELICOPTER)
				path = Paths.get(path+"\\Helicopters");
			DirectoryStream<Path> stream = Files.newDirectoryStream(path,
					"*.properties");
			for (Path entry : stream) {
				FileNameList.add(this.getPath() + File.separator
						+ entry.getFileName().toString());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String randomFile() { // return path + file name
		Random r = new Random();
		String res="";
		if (this.typeOfAircraft==TypeOfAircraft.AIRPLANE)
			res = FileNameList.get(r.nextInt(9)); // 9 files created
		else if (this.typeOfAircraft==TypeOfAircraft.HELICOPTER)
			res=FileNameList.get(r.nextInt(4)); // 4 files created
		return res;
	}

	public Path getPath() {
		return path;
	}
}
